import router from '@/router/index'
import store from '@/store/index'
import NProgress from 'nprogress' //  进度条
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']
// 前置
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
      // 判断有没有获取过用户资料
      if (!store.getters.userId) {
        await store.dispatch('user/getUserInfo')
      }
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from) => {
  NProgress.done()
})
